/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.sys.dao.AccountStreamDao;
import com.thinkgem.jeesite.modules.sys.dao.TbProductMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbShopMapper;
import com.thinkgem.jeesite.modules.sys.entity.TbMpWalletStream;
import com.thinkgem.jeesite.modules.sys.entity.TbShop;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 店铺Service
 * @author ThinkGem
 * @version 2014-05-16
 */
@Service
@Transactional(readOnly = true)
public class TbShopService extends CrudService<AccountStreamDao, TbMpWalletStream> {
	@Autowired
	private TbShopMapper tbShopMapper;

	private TbProductMapper tbProductMapper;




	@Transactional(readOnly = false)
	public TbShop saveStoreInfo(TbShop shop) {
		//先判断时候存在save 还是update
		TbShop currentShop = selectShop(shop);
		shop.setUpdateTime(new Date());
		if(currentShop!=null&&currentShop.getId()!=null){
			tbShopMapper.updateTbShop(shop);
		}else{
			//可以在当前登陆人的信息中得到代理商id，将代理商维护到tb_shop中
			shop.setAgentId(UserUtils.getUser().getAgentId());
			shop.setStatus("1");
			shop.setPackingFee(new BigDecimal("0.0"));
			shop.setCreateTime(new Date());
			tbShopMapper.save(shop);
			//把店铺id存放到商户的id中
			tbShopMapper.updateShop(shop);

		}
		return tbShopMapper.selectByUid(shop);
	}
	@Transactional(readOnly = false)
	public TbShop selectShop(TbShop shop) {
		shop = tbShopMapper.selectByUid(shop);
		if(shop!=null&&shop.getId()!=null){
			return shop;
		}else{
			//tbShopMapper.insert(shop);
			return new TbShop();
		}
	}
	@Transactional(readOnly = false)
	public TbShop selectByUidAndStatus(String  uid){
		TbShop tbShop=new TbShop();
		tbShop.setUid(uid);
		return tbShopMapper.selectByUidAndStatus(tbShop);
	}
	@Transactional(readOnly = false)
	public TbShop checkShopByCommunity(Integer communityId){
		TbShop tbShop=new TbShop();
		tbShop.setCommunityId(communityId);
		return tbShopMapper.checkShopByCommunity(tbShop);
	}
    @Transactional(readOnly = false)
	public TbShop updatePackingFee(TbShop shop) {
		tbShopMapper.updatePackingFee(shop);
		return shop;
	}
	@Transactional(readOnly = true)
	public Page<TbMpWalletStream> findWalletStreamPage(Page<TbMpWalletStream> page, TbMpWalletStream walletStream) {

		// 设置默认时间范围，默认当前月
		/*if (walletStream.getBeginDate() == null){
			walletStream.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
		}
		if (walletStream.getEndDate() == null){
			walletStream.setEndDate(DateUtils.addMonths(walletStream.getBeginDate(), 1));
		}*/
		Date date=walletStream.getEndDate();
		if(date!=null){
			date.setHours(23);
			date.setMinutes(59);
			date.setSeconds(59);
		}

		return super.findPage(page, walletStream);

	}
	/*@Transactional(readOnly = false)
	public void delete(Dict dict) {
		super.delete(dict);
		CacheUtils.remove(DictUtils.CACHE_DICT_MAP);
	}*/

	/**
	 * 店铺所属代理商商品列表。
	 */

}
